package com.fgbf.todo.Data.DB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Database Helper Class
 * Allows the Database to be accessed by the user
 * @author Warren Shenk
 * @date 2/20/2012
 */
public class DBHelper extends SQLiteOpenHelper {
	
	//////////////////////////////////////////////////////////////////////////////////
	// STATIC DATA
	//////////////////////////////////////////////////////////////////////////////////

	/**
	 * The name of the current database
	 */
	private static final String DATABASE_NAME = "TodoDB";
	/**
	 * The version of the current database
	 */
	private static final int DATABASE_VERSION = 17;
	
	/**
	 * The name of the User Database Table
	 */
	public static final String DB_USER_TABLE = "UserTable";
	
	/**
	 * The name of the Task Database Table
	 */
	public static final String DB_TASK_TABLE = "TaskTable";
	
	/**
	 * The name of the Category Table
	 */
	public static final String DB_CAT_TABLE = "CategoryTable";

	/**
	 * KEY for the row id
	 */
	public static final String KEY_ROWID = "_id";
	/**
	 * KEY for the username
	 */
	public static final String KEY_USERNAME = "user_name";
	/**
	 * KEY for the password
	 */
	public static final String KEY_PASSWORD = "user_password";
	/**
	 * KEY for task name
	 */
	public static final String KEY_TASKNAME = "task_name";
	/**
	 * KEY for task note
	 */
	public static final String KEY_TASKNOTE = "task_note";
	/**
	 * KEY for task priority
	 */
	public static final String KEY_TASKPRIOR = "task_priority";
	/**
	 * KEY for Category
	 */
	public static final String KEY_TASKCAT = "task_category";
	/**
	 * KEY for task completion
	 */
	public static final String KEY_TASKCOMP = "task_completed";
	/**
	 * KEY for task date
	 */
	public static final String KEY_TASKDATE = "task_date";
	/**
	 * KEY for task street
	 */
	public static final String KEY_TASKSTREET = "task_street";
	/**
	 * KEY for task city
	 */
	public static final String KEY_TASKCITY = "task_city";
	/**
	 * KEY for task state
	 */
	public static final String KEY_TASKSTATE = "task_state";
	/**
	 * KEY for task ZIP
	 */
	public static final String KEY_TASKZIP = "task_zip";

	
	//////////////////////////////////////////////////////////////////////////////////
	// CONSTRUCTOR
	//////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * Constructor, sends context, database name, and database version to the parent class 
	 * @param context
	 */
	public DBHelper(Context context)  {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	//--------------------------------------------------------------------------------

	/**
	 * Called when the database is created
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + DB_USER_TABLE + " (" + 
				KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
				KEY_USERNAME + " TEXT NOT NUll, " + KEY_PASSWORD + " TEXT NOT NULL);");
		db.execSQL("CREATE TABLE " + DB_TASK_TABLE + " (" +
				KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
				KEY_USERNAME + " TEXT NOT NULL, " + 
				KEY_TASKNAME + " TEXT NOT NULL, " +
				KEY_TASKNOTE + " TEXT NOT NULL, " +
				KEY_TASKPRIOR + " INTEGER NOT NULL, " + 
				KEY_TASKCAT + " TEXT NOT NULL, " +
				KEY_TASKCOMP + " INTEGER NOT NULL, " +
				KEY_TASKDATE + " INTEGER NOT NULL, " +
				KEY_TASKSTREET + " TEXT NOT NULL, " +
				KEY_TASKCITY + " TEXT NOT NULL, " + 
				KEY_TASKSTATE + " TEXT NOT NULL, " +
				KEY_TASKZIP + " TEXT NOT NULL);");
				
	}
	//--------------------------------------------------------------------------------

	/**
	 * Called when the database is upgraded
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		db.execSQL("DROP TABLE IF EXISTS " + DB_USER_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DB_TASK_TABLE);
		onCreate(db);

	}
	//--------------------------------------------------------------------------------
}
